﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using NASA.BeAMartian.Controls;
using NASA.BeAMartian.TagGraph.Surveying;
using System.Windows.Media.Imaging;
using BeAMartian.TagGraph.Surveying;

namespace NASA.BeAMartian.TagGraph.Surveying
{
    public partial class RoverTagSky : UserControl, IRoverTag
    {

        public delegate void InfoRequestedEventHandler(
           object sender,
           TagSelectedEventArgs e);

        public event InfoRequestedEventHandler MouseEnterInfo;
        public event InfoRequestedEventHandler MouseLeaveInfo;

        public static readonly DependencyProperty HeaderTextProperty =
            DependencyProperty.Register("HeaderText", typeof(string), typeof(RoverTagSky), new PropertyMetadata(new PropertyChangedCallback(HeaderText_Changed)));

        [System.ComponentModel.Category("HeaderText"), System.ComponentModel.Description("The first line of text.")]
        public string HeaderText
        {
            get { return (string)GetValue(HeaderTextProperty); }
            set { SetValue(HeaderTextProperty, value); }
        }

        private static void HeaderText_Changed(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
        {
            RoverTagSky eBtn = (RoverTagSky)dependencyObject;
            eBtn.UpdateText((string)eventArgs.NewValue);
        }

        public void UpdateText(string text)
        {
            if (header != null)
                setTextBlock(header, text);
        }

        public static readonly DependencyProperty ImageTagProperty =
            DependencyProperty.Register("ImageTag", typeof(ImageFeature), typeof(RoverTagSky), new PropertyMetadata(null));

        [System.ComponentModel.Category("ImageTag"), System.ComponentModel.Description("The image tag.")]
        public ImageFeature ImageTag
        {
            get { return (ImageFeature)GetValue(ImageTagProperty); }
            set { SetValue(ImageTagProperty, value); }
        }

        TextBlock header;

        private void setTextBlock(TextBlock tb, string text)
        {
            if (text.Contains(" "))
            {
                tb.Inlines.Clear();
                foreach (string s in text.Split(Convert.ToChar(" ")))
                {
                    if (tb.Inlines.Count > 0 && s.Length > 1)
                        tb.Inlines.Add(new LineBreak());

                    tb.Inlines.Add(new Run() { Text = s.Length == 1 ? s.PadLeft(2) : s });
                }
            }
            else
                tb.Text = text;
        }

        public RoverTagSky()
        {
            // Required to initialize variables
            InitializeComponent();
        }

        private void InfoIcon_MouseEnter(object sender, MouseEventArgs e)
        {
            if (MouseEnterInfo != null)
                MouseEnterInfo(sender, new TagSelectedEventArgs(ImageTag));
        }

        private void InfoIcon_MouseLeave(object sender, MouseEventArgs e)
        {
            if (MouseLeaveInfo != null)
                MouseLeaveInfo(sender, new TagSelectedEventArgs(ImageTag));
        }

        private void Image_Loaded(object sender, RoutedEventArgs e)
        {
            Image img = sender as Image;
            img.Source = new BitmapImage(new Uri(ImageTag.TagType.ToString() + ".png", UriKind.RelativeOrAbsolute));

            if (ImageTag.TagType != ImageTagTypes.Land)
            {
                img.Height = 16;
                img.Width = 16;
            }
        }

        Border brd;

        private void border_Loaded(object sender, RoutedEventArgs e)
        {
            //brd = sender as Border;
            //colorBackground(185);

            //if (ImageTag.TagType == ImageTagTypes.Land)
            //    brd.Background = new SolidColorBrush(Color.FromArgb(200, 167, 59, 11));
        }

        private void ToggleButton_Checked(object sender, RoutedEventArgs e)
        {
            tb.Unchecked -= ToggleButton_Unchecked;
            if (!_toggleEnabled)
                tb.IsChecked = false;
            tb.Unchecked += ToggleButton_Unchecked;
            //colorBackground(255);
        }

        private void ToggleButton_Unchecked(object sender, RoutedEventArgs e)
        {
            tb.Checked -= ToggleButton_Checked;
            if (!_toggleEnabled)
                tb.IsChecked = true;
            tb.Checked += ToggleButton_Checked;
            ////colorBackground(185);

            //if (ImageTag.TagType == ImageTagTypes.Land)
            //    brd.Background = new SolidColorBrush(Color.FromArgb(200, 167, 59, 11));
        }

        void tb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!_toggleEnabled)
                e.Handled = true;
        }

        private bool _toggleEnabled = true;
        public bool ToggleEnabled
        {
            get
            {
                return _toggleEnabled;
            }
            set
            {
                _toggleEnabled = value;
            }
        }

        void colorBackground(byte opacity)
        {
            if (ImageTag.TagType == ImageTagTypes.Sky)
                brd.Background = new SolidColorBrush(Color.FromArgb(opacity, 9, 109, 226));

            if (ImageTag.TagType == ImageTagTypes.Rover)
                brd.Background = new SolidColorBrush(Color.FromArgb(opacity, 19, 124, 28));

            if (ImageTag.TagType == ImageTagTypes.Land)
                brd.Background = new SolidColorBrush(Color.FromArgb(opacity, 167, 59, 11));
        }

        private void textbox_Loaded(object sender, RoutedEventArgs e)
        {
            setTextBlock(sender as TextBlock, HeaderText);
        }
    }
}